﻿using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace PlsqlFormat
{
    public static class StringUtil
    {
        public const int INDEX_NOT_FOUND = -1;

        public static int IndexOfIgnoreCase(this string str, string searchStr, int start = 0)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return INDEX_NOT_FOUND;
            }
            if (start < 0)
            {
                return INDEX_NOT_FOUND;
            }
            int endLimit = str.Length - searchStr.Length + 1;
            if (start > endLimit || str.Length <= start)
            {
                return INDEX_NOT_FOUND;
            }
            if (searchStr.Length == 0)
            {
                return start;
            }
            var res = str.Substring(start).ToLower();
            return res.IndexOf(searchStr.ToLower());
        }

        public static int LastIndexOfIgnoreCase(this string str, string searchStr, int start = 0)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return INDEX_NOT_FOUND;
            }
            if (start > str.Length - searchStr.Length)
            {
                start = str.Length - searchStr.Length;
            }
            if (start < 0 || str.Length <= start)
            {
                return INDEX_NOT_FOUND;
            }
            if (searchStr.Length == 0)
            {
                return start;
            }
            var res = str.Substring(start).ToLower();
            return res.LastIndexOf(searchStr.ToLower());
        }

        public static bool ContainsIgnoreCase(this string str, string searchStr)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return false;
            }
            return str.ToLower().Contains(searchStr.ToLower());
        }

        public static bool StartsWithIgnoreCase(this string str, string searchStr)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return false;
            }
            return str.ToLower().StartsWith(searchStr.ToLower());
        }


        public static bool EndWithIgnoreCase(this string str, string searchStr)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return false;
            }
            return str.ToLower().EndsWith(searchStr.ToLower());
        }

        public static bool EqualsIgnoreCase(this string str, string searchStr)
        {
            if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(searchStr))
            {
                return false;
            }
            if (str.Length != searchStr.Length)
            {
                return false;
            }
            return str.ToLower().Equals(searchStr.ToLower());
        }

        public static string ReplaceFirst(this string value, string oldValue, string newValue)
        {
            if (string.IsNullOrEmpty(oldValue))
                return value;

            int idx = value.IndexOfIgnoreCase(oldValue);
            if (idx == -1)
                return value;
            value = value.Remove(idx, oldValue.Length);
            return value.Insert(idx, newValue);
        }

        public static string RelaceLast(this string value , string oldValue, string newValue)
        {
            if (string.IsNullOrEmpty(oldValue))
                return value;

            int idx = value.LastIndexOfIgnoreCase(oldValue);
            if (idx == -1)
                return value;
            value = value.Remove(idx, oldValue.Length);
            return value.Insert(idx, newValue);
        }

        public static string ReplaceIgnoreCase(this string value, string oldValue, string newValue)
        {
            if (string.IsNullOrEmpty(oldValue))
                return value;

            oldValue = oldValue.ToLower();
            int start = 0;
            int end = value.IndexOfIgnoreCase(oldValue, start);
            if (end == INDEX_NOT_FOUND)
            {
                return value;
            }
            return Regex.Replace(value, oldValue, newValue, RegexOptions.IgnoreCase);
        }

        public static string[] SplitIgnoreCase(this string value,string pattern)
        {
            if (value.IndexOfIgnoreCase(pattern) == -1)
            {
                return new string[] { value };
            }
            return Regex.Split(value, pattern, RegexOptions.IgnoreCase);
        }

        public static int ContainsNumIgnoreCase(this string value, string pattern,bool IgnoreCase=true)
        {
            if (IgnoreCase)
            {
                return Regex.Matches(value, pattern, RegexOptions.IgnoreCase).Count;
            }
            else
            {
                return Regex.Matches(value, pattern, RegexOptions.None).Count;
            }
        }

        public static int IndexOfCount(this string value,string pattern, int count=0)
        {
            var rep =string.Empty;
            int idx =value.IndexOfIgnoreCase(pattern);
            if (idx == -1) return -1;
            for(int i = 0; i < pattern.Length; i++)
            {
                rep += "#";
            }
            for(int i = 0; i < count; i++)
            {
                value = value.ReplaceFirst(pattern, rep);  
            }
            return value.IndexOfIgnoreCase(pattern);
        }

        public static string SubStr(this string value,int start,int len)
        {
            if(len == -1)
            {
                len = value.Length;
            }
            return value.Substring(start, len);
        }
    }
}
